<?php

namespace Frieza\Modules\ConstructionPurchase\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Doraemon\model\ConstructionPurchase\ConstructionSubjectColumns;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class SubjectColumnsController extends Controller
{
    public function index(Content $content, Request $request)
    {
        $list = ConstructionSubjectColumns::selectListByWhere(['status[!]' => ConstructionSubjectColumns::STATUS_DELETE, 'ORDER' => ['sort' => 'ASC', 'id' => 'ASC']]);
        $gridModel = new GridDataModel($list);
        $grid = Admin::grid($gridModel,
            function (Grid $grid) {
                $grid->disableFilter();
                $grid->disablePagination();
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->column('id', 'ID')->sort();;
                $grid->column('desc', '描述');
                $grid->column('status', '状态')->switch([
                    'on' => ['value' => ConstructionSubjectColumns::STATUS_NORMAL, 'text' => '展示', 'color' => 'primary'],
                    'off' => ['value' => ConstructionSubjectColumns::STATUS_HIDE, 'text' => '隐藏', 'color' => 'default'],
                ]);
                $grid->column('cover_img_url', '背景图')->image();
                $grid->column('link', '链接地址')->linkSelect(false);
            }
        );

        return $content
            ->header('原创栏目列表')
            ->breadcrumb(['text' => '原创栏目列表'])
            ->row($grid->render());
    }

    public function edit(Content $content, Request $request, $id = 0)
    {
        $info = [];
        if ($id) {
            $info = ConstructionSubjectColumns::getInfoByWhere(['id' => $id]);
        }
        $form = new Form($info);
        $form->hidden('id', 'ID');
        $form->text('desc', '描述')->setWidth(4, 2);
        $form->imgUpload('cover_img', '封面图')->help('封面图必传');
        $form->linkSelect('link', '跳转链接');
        $form->action(url('admin/constructionPurchase/subject/index/save'));
        return $content->header('原创栏目配置')->breadcrumb(
            ['text' => '原创栏目列表', 'url' => url('admin/constructionPurchase/subject/index')],
            ['text' => '原创栏目配置'])
            ->row($form->render());
    }

    public function save(Content $content, Request $request)
    {
        $id = $request->input('id', 0);
        $desc = $request->input('desc', '');
        $coverImg = $request->input('cover_img', '');
        $linkType = $request->input('link_type', '');
        $linkParams = $request->input('link_params', '');

        $data = [
            'desc' => $desc,
            'cover_img' => $coverImg,
            'link_type' => $linkType,
            'link_params' => json_encode($linkParams, JSON_UNESCAPED_UNICODE),
            'status' => ConstructionSubjectColumns::STATUS_NORMAL,
        ];
        if ($id) {
            $id = ConstructionSubjectColumns::updateData($data, ['id' => $id]);
        } else {
            $id = ConstructionSubjectColumns::addData($data);
        }
        if ($id) {
            $success = new MessageBag([
                'title' => '操作成功'
            ]);
            return redirect(url('/admin/constructionPurchase/subject/index'))->with(compact('success'));
        } else {
            $error = new MessageBag([
                'title' => '操作失败'
            ]);
            return back()->withInput()->with(compact('error'));
        }
    }

    public function del(Content $content, Request $request, $id)
    {
        $data = [
            'status' => ConstructionSubjectColumns::STATUS_DELETE
        ];
        $rs = ConstructionSubjectColumns::updateData($data, ['id' => $id]);
        if ($rs) {
            return response()->json([
                'status' => true,
                'message' => "操作成功",
            ]);
        } else {
            return response()->json([
                'status' => false,
                'message' => "操作失败",
            ]);
        }
    }

    public function sort(Content $content, Request $request)
    {
        $sortInfo = $request->input('sort', null);
        if ($sortInfo && is_array($sortInfo)) {
            foreach ($sortInfo as $key => $val) {
                foreach ($sortInfo as $key => $val) {
                    ConstructionSubjectColumns::updateData(['sort' => $val], ['id' => $key]);
                }
            }
        }
    }

    public function switch(Content $content, Request $request, $id)
    {
        $status = $request->input('status', null);
        if (!is_null($status)) {
            $status = $status ? ConstructionSubjectColumns::STATUS_NORMAL : ConstructionSubjectColumns::STATUS_HIDE;
            $result = ConstructionSubjectColumns::updateData(['status' => $status], ['id' => $id]);
            if ($result) {
                return response()->json([
                    'status' => true,
                    'message' => "success",
                ]);
            } else {
                return response()->json([
                    'status' => false,
                    'message' => "修改失败",
                ]);
            }
        } else {
            return response()->json([
                'status' => false,
                'message' => "修改失败",
            ]);
        }
    }

}